###########################
#DATA ON POLITICAL PARTIES#
###########################

#MARIANA CARMO DUARTE

#AUGUST 2024

library(haven)
library(foreign)
library(scales)
library(dplyr)

setwd("")
polparties <- read.spss("CHES_ALL.sav", use.value.labels = TRUE, to.data.frame = TRUE)

#NOTE: This dataset includes data from 1984

#RECODE VARIABLE EU-SALIENCE
#Note: The variable ranges from 1 to 5 from 1984 to 1999

#0-10
polparties$eusal <- (polparties$eu_salience - 1) / 4 * 10
polparties$eu_salience_r1 <- polparties$eusal
polparties$eu_salience_r1[polparties$year==1999] <- polparties$eu_salience[polparties$year==1999]
polparties$eu_salience_r1[polparties$year==2002] <- polparties$eu_salience[polparties$year==2002]
polparties$eu_salience_r1[polparties$year==2006] <- polparties$eu_salience[polparties$year==2006]
polparties$eu_salience_r1[polparties$year==2010] <- polparties$eu_salience[polparties$year==2010]
polparties$eu_salience_r1[polparties$year==2014] <- polparties$eu_salience[polparties$year==2014]
polparties$eu_salience_r1[polparties$year==2017] <- polparties$eu_salience[polparties$year==2017]
polparties$eu_salience_r1[polparties$year==2019] <- polparties$eu_salience[polparties$year==2019]
summary(polparties$eu_salience) #ranges from 1 to 10
summary(polparties$eu_salience_r1) #ranges from 0 to 10

#0-1
polparties$eu_salience_r2 <- rescale(polparties$eu_salience_r1) 
summary(polparties$eu_salience_r1) #ranges from 0 to 10
summary(polparties$eu_salience_r2) #ranges from 0 to 1


#RECODE VARIABLE EU-POSITION

#0-10
polparties$eu_position_r1 <- (polparties$eu_position - 1) / 6 * 10
summary(polparties$eu_position) #ranges from 1 to 7
summary(polparties$eu_position_r1) #ranges from 0 to 10

#0-1
polparties$eu_position_r2 <- rescale(polparties$eu_position) 
summary(polparties$eu_position) #ranges from 1 to 7
summary(polparties$eu_position_r2) #ranges from 0 to 1


#RECODE VARIABLE EU-DISSENT
#Note: The variable ranges from 1 to 5 from 1984 to 1999
#Note: Not used in this paper

#0-10
polparties$eudis <- (polparties$eu_dissent - 1) / 4 * 10
polparties$eu_dissent_r1 <- polparties$eudis
polparties$eu_dissent_r1[polparties$year==1999] <- polparties$eu_dissent[polparties$year==1999]
polparties$eu_dissent_r1[polparties$year==2002] <- polparties$eu_dissent[polparties$year==2002]
polparties$eu_dissent_r1[polparties$year==2006] <- polparties$eu_dissent[polparties$year==2006]
polparties$eu_dissent_r1[polparties$year==2010] <- polparties$eu_dissent[polparties$year==2010]
polparties$eu_dissent_r1[polparties$year==2014] <- polparties$eu_dissent[polparties$year==2014]
polparties$eu_dissent_r1[polparties$year==2017] <- polparties$eu_dissent[polparties$year==2017]
polparties$eu_dissent_r1[polparties$year==2019] <- polparties$eu_dissent[polparties$year==2019]
summary(polparties$eu_dissent) #ranges from 0 to 10
summary(polparties$eu_dissent_r1) #ranges from 0 to 10

#0-1
polparties$eu_dissent_r2 <- polparties$eu_dissent_r1 / 10
summary(polparties$eu_dissent_r1) #ranges from 0 to 10
summary(polparties$eu_dissent_r2) #ranges from 0 to 1


#LOAD PARLGOV DATA
setwd("")
polpargov <- read.spss("Elections_PARLGOV.sav", use.value.labels = TRUE, to.data.frame = TRUE)

#Create the grouping variable "country_electionyear"
polpargov$country_electionyear <- paste0(polpargov$country_name_short, polpargov$election_year)
polparties$country_electionyear <- paste0(polparties$country, polparties$electionyear)
polparties$country_year <- paste0(polparties$country, polparties$year)

#ELECTION ID - PARLGOV

polparties$election_id <- 0

#Austria
polparties$election_id[polparties$country_electionyear=="aus 1983"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1983"])
polparties$election_id[polparties$country_electionyear=="aus 1986"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1986"])
polparties$election_id[polparties$country_electionyear=="aus 1990"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1990"])
polparties$election_id[polparties$country_electionyear=="aus 1995"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1995"])
polparties$election_id[polparties$country_electionyear=="aus 1999"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1999"])
polparties$election_id[polparties$country_electionyear=="aus 2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT2002"])
polparties$election_id[polparties$country_electionyear=="aus 2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT2006"])
polparties$election_id[polparties$country_electionyear=="aus 2008"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT2008"])
polparties$election_id[polparties$country_electionyear=="aus 2013"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT2013"])
polparties$election_id[polparties$country_electionyear=="aus 2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT2019"])

#Belgium
polparties$election_id[polparties$country_electionyear=="be  1981"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1981"])
polparties$election_id[polparties$country_electionyear=="be  1987"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1987"])
polparties$election_id[polparties$country_electionyear=="be  1991"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1991"])
polparties$election_id[polparties$country_electionyear=="be  1995"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1995"])
polparties$election_id[polparties$country_electionyear=="be  1999"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1999"])
polparties$election_id[polparties$country_electionyear=="be  2003"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL2003"])
polparties$election_id[polparties$country_electionyear=="be  2010"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL2010"])
polparties$election_id[polparties$country_electionyear=="be  2014"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL2014"])
polparties$election_id[polparties$country_electionyear=="be  2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL2019"])

#Bulgaria
polparties$election_id[polparties$country_electionyear=="bul 2001"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BGR2001"])
polparties$election_id[polparties$country_electionyear=="bul 2005"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BGR2005"])
polparties$election_id[polparties$country_electionyear=="bul 2009"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BGR2009"])
polparties$election_id[polparties$country_electionyear=="bul 2014"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BGR2014"])
polparties$election_id[polparties$country_electionyear=="bul 2017"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BGR2017"])

#Croatia
polparties$election_id[polparties$country_electionyear=="cro 2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HRV2011"])
polparties$election_id[polparties$country_electionyear=="cro 2016"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HRV2016"])

#Cyrpus
polparties$election_id[polparties$country_electionyear=="cyp 2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CYP2011"])
polparties$election_id[polparties$country_electionyear=="cyp 2016"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CYP2016"])

#Czech Republic
polparties$election_id[polparties$country_electionyear=="cz  2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CZE2002"])
polparties$election_id[polparties$country_electionyear=="cz  2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CZE2006"])
polparties$election_id[polparties$country_electionyear=="cz  2010"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CZE2010"])
polparties$election_id[polparties$country_electionyear=="cz  2013"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CZE2013"])
polparties$election_id[polparties$country_year=="cz  2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CZE2017"])

#Denmark
polparties$election_id[polparties$country_electionyear=="dk  1984"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1984"])
polparties$election_id[polparties$country_electionyear=="dk  1988"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1988"])
polparties$election_id[polparties$country_electionyear=="dk  1990"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1990"])
polparties$election_id[polparties$country_electionyear=="dk  1994"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1994"])
polparties$election_id[polparties$country_electionyear=="dk  1998"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1998"])
polparties$election_id[polparties$country_electionyear=="dk  2001"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK2001"])
polparties$election_id[polparties$country_electionyear=="dk  2005"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK2005"])
polparties$election_id[polparties$country_electionyear=="dk  2007"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK2007"])
polparties$election_id[polparties$country_electionyear=="dk  2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK2011"])
polparties$election_id[polparties$country_electionyear=="dk  2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK2019"])

#Spain
polparties$election_id[polparties$country_electionyear=="esp 1982"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP1982"])
polparties$election_id[polparties$country_electionyear=="esp 1986"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP1986"])
polparties$election_id[polparties$country_electionyear=="esp 1989"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP1989"])
polparties$election_id[polparties$country_electionyear=="esp 1996"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP1996"])
polparties$election_id[polparties$country_electionyear=="esp 2000"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP2000"])
polparties$election_id[polparties$country_electionyear=="esp 2004"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP2004"])
polparties$election_id[polparties$country_electionyear=="esp 2008"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP2008"])
polparties$election_id[polparties$country_electionyear=="esp 2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP2011"])
polparties$election_id[polparties$country_electionyear=="esp 2016"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP2016"])
polparties$election_id[polparties$country_electionyear=="esp 2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP20199"])

#Estonia
polparties$election_id[polparties$country_electionyear=="est 2003"] <- mean(polpargov$election_id[polpargov$country_electionyear=="EST2003"])
polparties$election_id[polparties$country_electionyear=="est 2007"] <- mean(polpargov$election_id[polpargov$country_electionyear=="EST2007"])
polparties$election_id[polparties$country_electionyear=="est 2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="EST2011"])
polparties$election_id[polparties$country_electionyear=="est 2015"] <- mean(polpargov$election_id[polpargov$country_electionyear=="EST2015"])
polparties$election_id[polparties$country_electionyear=="est 2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="EST2019"])

#Finland
polparties$election_id[polparties$country_electionyear=="fin 1983"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1983"])
polparties$election_id[polparties$country_electionyear=="fin 1987"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1987"])
polparties$election_id[polparties$country_electionyear=="fin 1991"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1991"])
polparties$election_id[polparties$country_electionyear=="fin 1995"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1995"])
polparties$election_id[polparties$country_year=="fin 2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1999"])
polparties$election_id[polparties$country_electionyear=="fin 2003"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN2003"])
polparties$election_id[polparties$country_electionyear=="fin 2007"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN2007"])
polparties$election_id[polparties$country_electionyear=="fin 2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN2011"])
polparties$election_id[polparties$country_electionyear=="fin 2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN2019"])

#France
polparties$election_id[polparties$country_electionyear=="fr  1981"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA1981"])
polparties$election_id[polparties$country_electionyear=="fr  1988"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA1988"])
polparties$election_id[polparties$country_electionyear=="fr  1993"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA1993"])
polparties$election_id[polparties$country_electionyear=="fr  1997"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA1997"])
polparties$election_id[polparties$country_year=="fr  2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA2002"])
polparties$election_id[polparties$country_electionyear=="fr  2007"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA2007"])
polparties$election_id[polparties$country_electionyear=="fr  2012"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA2012"])
polparties$election_id[polparties$country_electionyear=="fr  2017"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA2017"])

#Germany
polparties$election_id[polparties$country_electionyear=="ge  1983"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1983"])
polparties$election_id[polparties$country_electionyear=="ge  1987"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1987"])
polparties$election_id[polparties$country_electionyear=="ge  1990"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1990"])
polparties$election_id[polparties$country_electionyear=="ge  1994"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1994"])
polparties$election_id[polparties$country_electionyear=="ge  1998"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1998"])
polparties$election_id[polparties$country_electionyear=="ge  2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU2002"])
polparties$election_id[polparties$country_electionyear=="ge  2005"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU2005"])
polparties$election_id[polparties$country_electionyear=="ge  2009"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU2009"])
polparties$election_id[polparties$country_electionyear=="ge  2013"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU2013"])
polparties$election_id[polparties$country_year=="ge  2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU2017"])

#Greece
polparties$election_id[polparties$country_electionyear=="gr  1981"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC1981"])
polparties$election_id[polparties$country_electionyear=="gr  1985"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC1985"])
polparties$election_id[polparties$country_electionyear=="gr  1990"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC1990"])
polparties$election_id[polparties$country_electionyear=="gr  1996"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC1996"])
polparties$election_id[polparties$country_electionyear=="gr  2000"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC2000"])
polparties$election_id[polparties$country_electionyear=="gr  2004"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC2004"])
polparties$election_id[polparties$country_electionyear=="gr  2009"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC2009"])
polparties$election_id[polparties$country_electionyear=="gr  2012"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC20122"])
polparties$election_id[polparties$country_electionyear=="gr  2015"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC20155"])
polparties$election_id[polparties$country_electionyear=="gr  2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC2019"])

#Hungary
polparties$election_id[polparties$country_electionyear=="hun 2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HUN2002"])
polparties$election_id[polparties$country_electionyear=="hun 2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HUN2006"])
polparties$election_id[polparties$country_electionyear=="hun 2010"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HUN2010"])
polparties$election_id[polparties$country_year=="hun 2014"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HUN2014"])
polparties$election_id[polparties$country_electionyear=="hun 2018"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HUN2018"])

#Ireland
polparties$election_id[polparties$country_electionyear=="irl 1982"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL19822"])
polparties$election_id[polparties$country_electionyear=="irl 1987"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL1987"])
polparties$election_id[polparties$country_electionyear=="irl 1992"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL1992"])
polparties$election_id[polparties$country_electionyear=="irl 1997"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL1997"])
polparties$election_id[polparties$country_year=="irl 2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL2002"])
polparties$election_id[polparties$country_electionyear=="irl 2007"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL2007"])
polparties$election_id[polparties$country_electionyear=="irl 2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL2011"])
polparties$election_id[polparties$country_electionyear=="irl 2016"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL2016"])

#Italy
polparties$election_id[polparties$country_electionyear=="it  1983"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA1983"])
polparties$election_id[polparties$country_electionyear=="it  1987"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA1987"])
polparties$election_id[polparties$country_electionyear=="it  1992"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA1992"])
polparties$election_id[polparties$country_electionyear=="it  1996"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA1996"])
polparties$election_id[polparties$country_electionyear=="it  2001"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA2001"])
polparties$election_id[polparties$country_electionyear=="it  2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA2006"])
polparties$election_id[polparties$country_electionyear=="it  2008"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA2008"])
polparties$election_id[polparties$country_year=="it  2014"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA2013"])
polparties$election_id[polparties$country_electionyear=="it  2018"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA2018"])

#Latvia
polparties$election_id[polparties$country_electionyear=="lat 2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LVA2002"])
polparties$election_id[polparties$country_electionyear=="lat 2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LVA2006"])
polparties$election_id[polparties$country_electionyear=="lat 2010"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LVA2010"])
polparties$election_id[polparties$country_electionyear=="lat 2014"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LVA2014"])
polparties$election_id[polparties$country_electionyear=="lat 2018"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LVA2018"])

#Lithuania
polparties$election_id[polparties$country_electionyear=="lith2000"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LTU2000"])
polparties$election_id[polparties$country_electionyear=="lith2004"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LTU2004"])
polparties$election_id[polparties$country_electionyear=="lith2008"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LTU2008"])
polparties$election_id[polparties$country_electionyear=="lith2012"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LTU2012"])
polparties$election_id[polparties$country_electionyear=="lith2016"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LTU2016"])

#Luxembourg
polparties$election_id[polparties$country_electionyear=="lux 2013"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LUX2013"])
polparties$election_id[polparties$country_electionyear=="lux 2018"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LUX2018"])

#Malta
polparties$election_id[polparties$country_electionyear=="mal 2013"] <- mean(polpargov$election_id[polpargov$country_electionyear=="MLT2013"])
polparties$election_id[polparties$country_electionyear=="mal 2017"] <- mean(polpargov$election_id[polpargov$country_electionyear=="MLT2017"])

#Netherlands
polparties$election_id[polparties$country_electionyear=="nl  1982"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1982"])
polparties$election_id[polparties$country_electionyear=="nl  1986"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1986"])
polparties$election_id[polparties$country_electionyear=="nl  1989"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1989"])
polparties$election_id[polparties$country_electionyear=="nl  1994"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1994"])
polparties$election_id[polparties$country_electionyear=="nl  1998"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1998"])
polparties$election_id[polparties$country_electionyear=="nl  2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD2002"])
polparties$election_id[polparties$country_electionyear=="nl  2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD2006"])
polparties$election_id[polparties$country_electionyear=="nl  2010"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD2010"])
polparties$election_id[polparties$country_electionyear=="nl  2012"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD2012"])
polparties$election_id[polparties$country_year=="nl  2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD2017"])

#Poland
polparties$election_id[polparties$country_electionyear=="pol 2001"] <- mean(polpargov$election_id[polpargov$country_electionyear=="POL2001"])
polparties$election_id[polparties$country_electionyear=="pol 2005"] <- mean(polpargov$election_id[polpargov$country_electionyear=="POL2005"])
polparties$election_id[polparties$country_electionyear=="pol 2007"] <- mean(polpargov$election_id[polpargov$country_electionyear=="POL2007"])
polparties$election_id[polparties$country_electionyear=="pol 2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="POL2011"])
polparties$election_id[polparties$country_electionyear=="pol 2015"] <- mean(polpargov$election_id[polpargov$country_electionyear=="POL2015"])
polparties$election_id[polparties$country_electionyear=="pol 2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="POL2019"])

#Portugal
polparties$election_id[polparties$country_electionyear=="por 1983"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT1983"])
polparties$election_id[polparties$country_electionyear=="por 1987"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT1987"])
polparties$election_id[polparties$country_electionyear=="por 1991"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT1991"])
polparties$election_id[polparties$country_electionyear=="por 1995"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT1995"])
polparties$election_id[polparties$country_electionyear=="por 1999"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT1999"])
polparties$election_id[polparties$country_electionyear=="por 2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT2002"])
polparties$election_id[polparties$country_electionyear=="por 2005"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT2005"])
polparties$election_id[polparties$country_electionyear=="por 2009"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT2009"])
polparties$election_id[polparties$country_electionyear=="por 2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT2011"])
polparties$election_id[polparties$country_electionyear=="por 2015"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT2015"])
polparties$election_id[polparties$country_electionyear=="por 2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT2019"])

#Romania
polparties$election_id[polparties$country_electionyear=="rom 2000"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ROU2000"])
polparties$election_id[polparties$country_electionyear=="rom 2004"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ROU2004"])
polparties$election_id[polparties$country_electionyear=="rom 2008"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ROU2008"])
polparties$election_id[polparties$country_electionyear=="rom 2012"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ROU2012"])
polparties$election_id[polparties$country_electionyear=="rom 2016"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ROU2016"])

#Slovenia
polparties$election_id[polparties$country_electionyear=="sle 2000"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVN2000"])
polparties$election_id[polparties$country_electionyear=="sle 2004"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVN2004"])
polparties$election_id[polparties$country_electionyear=="sle 2008"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVN2008"])
polparties$election_id[polparties$country_electionyear=="sle 2014"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVN2014"])
polparties$election_id[polparties$country_electionyear=="sle 2018"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVN2018"])

#Slovakia
polparties$election_id[polparties$country_electionyear=="slo 2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVK2002"])
polparties$election_id[polparties$country_electionyear=="slo 2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVK2006"])
polparties$election_id[polparties$country_electionyear=="slo 2010"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVK2010"])
polparties$election_id[polparties$country_electionyear=="slo 2012"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVK2012"])
polparties$election_id[polparties$country_year=="slo 2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVK2016"])

#Sweden
polparties$election_id[polparties$country_electionyear=="swe 1982"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1982"])
polparties$election_id[polparties$country_electionyear=="swe 1988"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1988"])
polparties$election_id[polparties$country_electionyear=="swe 1991"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1991"])
polparties$election_id[polparties$country_electionyear=="swe 1994"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1994"])
polparties$election_id[polparties$country_electionyear=="swe 1998"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1998"])
polparties$election_id[polparties$country_electionyear=="swe 2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE2002"])
polparties$election_id[polparties$country_electionyear=="swe 2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE2006"])
polparties$election_id[polparties$country_electionyear=="swe 2010"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE2010"])
polparties$election_id[polparties$country_year=="swe 2014"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE2014"])
polparties$election_id[polparties$country_electionyear=="swe 2018"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE2018"])

#United Kingdom
polparties$election_id[polparties$country_electionyear=="uk  1983"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR1983"])
polparties$election_id[polparties$country_electionyear=="uk  1987"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR1987"])
polparties$election_id[polparties$country_electionyear=="uk  1992"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR1992"])
polparties$election_id[polparties$country_electionyear=="uk  1997"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR1997"])
polparties$election_id[polparties$country_electionyear=="uk  2001"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR2001"])
polparties$election_id[polparties$country_electionyear=="uk  2005"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR2005"])
polparties$election_id[polparties$country_year=="uk  2010"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR2010"])
polparties$election_id[polparties$country_electionyear=="uk  2017"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR2017"])
polparties$election_id[polparties$country_electionyear=="uk  2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR2019"])


# COUNTRY ID - PARLGOV
polparties$country_id <- 0

polparties$country_id[polparties$cntry_n=="aus"] <- mean(polpargov$country_id[polpargov$country_name_short=="AUT"])
polparties$country_id[polparties$cntry_n=="be"] <- mean(polpargov$country_id[polpargov$country_name_short=="BEL"])
polparties$country_id[polparties$cntry_n=="bul"] <- mean(polpargov$country_id[polpargov$country_name_short=="BGR"])
polparties$country_id[polparties$cntry_n=="cro"] <- mean(polpargov$country_id[polpargov$country_name_short=="HRV"])
polparties$country_id[polparties$cntry_n=="cyp"] <- mean(polpargov$country_id[polpargov$country_name_short=="CYP"])
polparties$country_id[polparties$cntry_n=="cz"] <- mean(polpargov$country_id[polpargov$country_name_short=="CZE"])
polparties$country_id[polparties$cntry_n=="dk"] <- mean(polpargov$country_id[polpargov$country_name_short=="DNK"])
polparties$country_id[polparties$cntry_n=="esp"] <- mean(polpargov$country_id[polpargov$country_name_short=="ESP"])
polparties$country_id[polparties$cntry_n=="est"] <- mean(polpargov$country_id[polpargov$country_name_short=="EST"])
polparties$country_id[polparties$cntry_n=="fin"] <- mean(polpargov$country_id[polpargov$country_name_short=="FIN"])
polparties$country_id[polparties$cntry_n=="fr"] <- mean(polpargov$country_id[polpargov$country_name_short=="FRA"])
polparties$country_id[polparties$cntry_n=="ge"] <- mean(polpargov$country_id[polpargov$country_name_short=="DEU"])
polparties$country_id[polparties$cntry_n=="gr"] <- mean(polpargov$country_id[polpargov$country_name_short=="GRC"])
polparties$country_id[polparties$cntry_n=="hun"] <- mean(polpargov$country_id[polpargov$country_name_short=="HUN"])
polparties$country_id[polparties$cntry_n=="irl"] <- mean(polpargov$country_id[polpargov$country_name_short=="IRL"])
polparties$country_id[polparties$cntry_n=="it"] <- mean(polpargov$country_id[polpargov$country_name_short=="ITA"])
polparties$country_id[polparties$cntry_n=="lat"] <- mean(polpargov$country_id[polpargov$country_name_short=="LVA"])
polparties$country_id[polparties$cntry_n=="lith"] <- mean(polpargov$country_id[polpargov$country_name_short=="LTU"])
polparties$country_id[polparties$cntry_n=="lux"] <- mean(polpargov$country_id[polpargov$country_name_short=="LUX"])
polparties$country_id[polparties$cntry_n=="mal"] <- mean(polpargov$country_id[polpargov$country_name_short=="MLT"])
polparties$country_id[polparties$cntry_n=="nl"] <- mean(polpargov$country_id[polpargov$country_name_short=="NLD"])
polparties$country_id[polparties$cntry_n=="pol"] <- mean(polpargov$country_id[polpargov$country_name_short=="POL"])
polparties$country_id[polparties$cntry_n=="por"] <- mean(polpargov$country_id[polpargov$country_name_short=="PRT"])
polparties$country_id[polparties$cntry_n=="rom"] <- mean(polpargov$country_id[polpargov$country_name_short=="ROU"])
polparties$country_id[polparties$cntry_n=="sle"] <- mean(polpargov$country_id[polpargov$country_name_short=="SVN"])
polparties$country_id[polparties$cntry_n=="slo"] <- mean(polpargov$country_id[polpargov$country_name_short=="SVK"])
polparties$country_id[polparties$cntry_n=="swe"] <- mean(polpargov$country_id[polpargov$country_name_short=="SWE"])
polparties$country_id[polparties$cntry_n=="uk"] <- mean(polpargov$country_id[polpargov$country_name_short=="GBR"])

setwd("")
partyPGCHES <- read.csv2("partyfacts_CHES_PARLGOV.csv")

#PARTY ID - PARLGOV
partyPGCHES <- subset(partyPGCHES, select =c(party_id, dataset_party_id.y, partyfacts_id))
polparties <- merge(polparties, partyPGCHES, by = "party_id", all = TRUE)
polparties <- filter(polparties, !is.na(cntry_n))

setwd("")
write_sav(polparties, "PoliticalParties_CHES.sav")
write.csv2(polparties, "PoliticalParties_CHES.csv")
